package com.helloniu.fl.widget {

import com.helloniu.fl.util.Tool;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.EventPhase;
import flash.utils.ByteArray;
import mx.controls.Image;

public class ByteArrayImage extends Image {
    //public static const SIZE_CHANGEED:String = "SIZE_CHANGEED";

    public function ByteArrayImage() {
        super();

        addEventListener(Event.UNLOAD, unloadedComplete); 
    }

    public function setData(value:ByteArray):void {    
        var loader:Loader = new Loader();            
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
        loader.loadBytes(value);
        this.source = loader;
        this.percentWidth = 100;
        this.percentHeight = 100;
        this.width = 10;
        this.height = 10;
    }

    private function loadComplete(event:Event):void {
        var val:LoaderInfo = event.currentTarget as LoaderInfo;

        if(val != null) {
            this.width = Math.floor(val.content.width);
            this.height = Math.floor(val.content.height);
            
            //dispatchEvent(new Event(SIZE_CHANGEED));
            dispatchEvent(new Event(Event.COMPLETE));
        }
    }

    private function unloadedComplete(e:Event):void {        
    }
    
}
}